<template>
	<xn-form-container
		:title="formData.id ? '编辑大模型接口配置表' : '增加大模型接口配置表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="配置名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入配置名称" allow-clear />
			</a-form-item>
			<a-form-item label="模型供应商：" name="provider">
				<a-input v-model:value="formData.provider" placeholder="请输入模型供应商" allow-clear />
			</a-form-item>
			<a-form-item label="模型名称：" name="modelName">
				<a-input v-model:value="formData.modelName" placeholder="请输入模型名称" allow-clear />
			</a-form-item>
			<a-form-item label="模型部署名称：" name="modelDeployment">
				<a-input v-model:value="formData.modelDeployment" placeholder="请输入模型部署名称" allow-clear />
			</a-form-item>
			<a-form-item label="API接口地址：" name="apiUrl">
				<a-input v-model:value="formData.apiUrl" placeholder="请输入API接口地址" allow-clear />
			</a-form-item>
			<a-form-item label="API密钥：" name="apiKey">
				<a-input v-model:value="formData.apiKey" placeholder="请输入API密钥" allow-clear />
			</a-form-item>
			<a-form-item label="API密钥secret：" name="apiSecret">
				<a-input v-model:value="formData.apiSecret" placeholder="请输入API密钥secret" allow-clear />
			</a-form-item>
			<a-form-item label="最大token数：" name="maxTokens">
				<a-input v-model:value="formData.maxTokens" placeholder="请输入最大token数" allow-clear />
			</a-form-item>
			<a-form-item label="温度系数：" name="temperature">
				<a-input v-model:value="formData.temperature" placeholder="请输入温度系数" allow-clear />
			</a-form-item>
			<a-form-item label="TOP_P采样值：" name="topP">
				<a-input v-model:value="formData.topP" placeholder="请输入TOP_P采样值" allow-clear />
			</a-form-item>
			<a-form-item label="存在惩罚系数：" name="presencePenalty">
				<a-input v-model:value="formData.presencePenalty" placeholder="请输入存在惩罚系数" allow-clear />
			</a-form-item>
			<a-form-item label="频率惩罚系数：" name="frequencyPenalty">
				<a-input v-model:value="formData.frequencyPenalty" placeholder="请输入频率惩罚系数" allow-clear />
			</a-form-item>
			<a-form-item label="响应格式(如json)：" name="responseFormat">
				<a-input v-model:value="formData.responseFormat" placeholder="请输入响应格式(如json)" allow-clear />
			</a-form-item>
			<a-form-item label="是否流式响应(0-否,1-是)：" name="stream">
				<a-input v-model:value="formData.stream" placeholder="请输入是否流式响应(0-否,1-是)" allow-clear />
			</a-form-item>
			<a-form-item label="系统提示词：" name="systemMessage">
				<a-input v-model:value="formData.systemMessage" placeholder="请输入系统提示词" allow-clear />
			</a-form-item>
			<a-form-item label="停止序列：" name="stopSequences">
				<a-input v-model:value="formData.stopSequences" placeholder="请输入停止序列" allow-clear />
			</a-form-item>
			<a-form-item label="超时时间(秒)：" name="timeout">
				<a-input v-model:value="formData.timeout" placeholder="请输入超时时间(秒)" allow-clear />
			</a-form-item>
			<a-form-item label="代理服务器地址：" name="proxyHost">
				<a-input v-model:value="formData.proxyHost" placeholder="请输入代理服务器地址" allow-clear />
			</a-form-item>
			<a-form-item label="代理服务器端口：" name="proxyPort">
				<a-input v-model:value="formData.proxyPort" placeholder="请输入代理服务器端口" allow-clear />
			</a-form-item>
			<a-form-item label="状态（1-启用，2-禁用）：" name="status">
				<a-input v-model:value="formData.status" placeholder="请输入状态（1-启用，2-禁用）" allow-clear />
			</a-form-item>
			<a-form-item label="排序：" name="sort">
				<a-input v-model:value="formData.sort" placeholder="请输入排序" allow-clear />
			</a-form-item>
			<a-form-item label="删除标志（0-未删除，1-已删除）：" name="delFlag">
				<a-input v-model:value="formData.delFlag" placeholder="请输入删除标志（0-未删除，1-已删除）" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="aiLlmConfigForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import aiLlmConfigApi from '@/api/ai/aiLlmConfigApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				aiLlmConfigApi
					.aiLlmConfigSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
